<?php
	namespace Model;
	class Model {
		private static $_instance=null;
		private $pdo=null;
		private $driver='mysql';
		private $host='localhost';
		private $dbname='tli';
		private $user='tli';
		private $pwd='tli';

		private function __construct(){
			try {
				$this->pdo = new \PDO($this->driver.':host='.$this->host.';dbname='.$this->dbname,$this->user, $this->pwd);
			}catch (PDOException $e){
				throw $e;
			}
		}
	
		public static function getInstance(){
			if(is_null(self::$_instance)) {
				try{
					self::$_instance = new Model();
				}catch(Exeption $e){
					echo 'Erreur : '.$e->getMessage().'<br />';
					echo 'N° : '.$e->getCode();
					self::$_instance = null;
				}
			}
			return self::$_instance;
		}


		public function getAllPosts(){
			$posts = Array();
			$ps=$this->pdo->prepare('SELECT post.id, user.id as userId, user.pseudo AS userPseudo, DATE_FORMAT(post.date, "%a, %d %M %Y %H:%i:%s +0100") AS date,post.message FROM post JOIN user ON post.user = user.id ORDER BY id DESC');
			$ps->execute();
			while($post = $ps->fetchObject('Model\\Post')){
				$posts[] = $post;
			}
			return $posts;
		}
		public function getPostsFrom($userId){
			$posts = Array();
			$ps=$this->pdo->prepare('SELECT post.id, user.id as userId, user.pseudo AS userPseudo, DATE_FORMAT(post.date, "%a, %d %M %Y %H:%i:%s +0100") AS date,post.message FROM post JOIN user ON post.user = user.id WHERE user.id=? ORDER BY id DESC');
			$ps->execute(array($userId));
			while($post = $ps->fetchObject('Model\\Post')){
				$posts[] = $post;
			}
			return $posts;
		}
		public function getPost($id){
			$ps=$this->pdo->prepare('SELECT post.id, user.id as userId, user.pseudo AS userPseudo, DATE_FORMAT(post.date, "%a, %d %M %Y %H:%i:%s +0100") AS date,post.message FROM post JOIN user ON post.user = user.id WHERE post.id=? ORDER BY id DESC');
			$ps->execute(array($id));
			return $ps->fetchObject('Model\\Post');
		}
		public function postExists($id){
			return ($this->getPost($id)!=null);
		}


		public function getAllUsers(){
			$users = Array();
			$ps=$this->pdo->prepare('SELECT * FROM user');
			$ps->execute();
			while($user = $ps->fetchObject('Model\\User')){
				$users[] = $user;
			}
			return $users;
		}

		public function getUser($id){
			$ps=$this->pdo->prepare('SELECT * FROM user WHERE id = ?');
			$ps->execute(Array($id));
			return $ps->fetchObject('Model\\User');
		}
		public function userExists($id){
			return ($this->getUser($id)!=null);
		}

		public function postMessage($pseudo,$message){
			$userId = $this->getUserIdByPseudo($pseudo);
			$ps=$this->pdo->prepare('INSERT INTO post (user,message) VALUES (?,?)');
			$ps->execute(Array($userId,$message));
			return $this->pdo->lastInsertId();
		}

		private function getUserIdByPseudo($pseudo){
			$ps=$this->pdo->prepare('SELECT * FROM user WHERE pseudo = ?');
			$ps->execute(Array($pseudo));
			$user = $ps->fetchObject('Model\\User');
			if($user == null){
				return $this->createUser($pseudo);
			}
			return $user->id;
		}

		private function createUser($pseudo){
			$ps=$this->pdo->prepare('INSERT INTO user (pseudo) VALUES (?)');
			$ps->execute(Array($pseudo));
			return $this->pdo->lastInsertId();
		}
	}
?>
